﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WareHouse.Domain.ModelClass;
using WareHouse.Repositories;

namespace WareHose.Application.MarketFile
{
    using AutoMapper;
    using Microsoft.AspNetCore.Mvc;
    using Panda.DynamicWebApi;
    using Panda.DynamicWebApi.Attributes;
    using SqlSugar;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using WareHouse.Domain.ModelClass;
    using WareHouse.Repositories;

    namespace WareHose.Application.WareHouseRomve
    {
        /// <summary>
        /// 出库管理
        /// </summary>
        [DynamicWebApi]
        [ApiExplorerSettings(GroupName = "v1")]
        public class MarketFile : IDynamicWebApi
        {
            private readonly IBaseRepository<Market> sRepository;
            private readonly ISqlSugarClient sugarClient;
            private readonly IMapper mapper;
            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="sRepository"></param>
            /// <param name="sugarClient"></param>
            /// <param name="mapper"></param>
            public MarketFile(IBaseRepository<Market> sRepository, ISqlSugarClient sugarClient, IMapper mapper)
            {
                this.mapper = mapper;
                this.sRepository = sRepository;
                this.sugarClient = sugarClient;
            }
            /// <summary>
            /// 新增移库
            /// </summary>
            /// <param name="Market"></param>
            /// <returns></returns>
            [HttpPost]

            public async Task<ResultDto> AddWareHouseRomve(Market Market)
            {
                try
                {

                    if (await sRepository.FistAsync(x => x.MarketId == Market.MarketId) == null)
                    {

                        if (await sRepository.InsertAsync(Market) != 0)
                        {
                            return new ResultDto { Code = Status.Success, Message = "添加成功" };
                        }
                        else
                        {
                            return new ResultDto { Code = Status.Success, Message = "添加失败" };
                        }

                    }
                    else
                    {
                        return new ResultDto { Code = Status.Success, Message = "此出库记录已存在" };
                    }


                }
                catch (Exception e)
                {

                    return new ResultDto { Code = Status.Failed, Message = e.Message };
                }
            }
            /// <summary>
            /// 移库条件分页显示
            /// </summary>
            /// <returns></returns>
            [HttpGet]

            public async Task<ResultDto<PagingDto<Market>>> ShowWareHouseRomve(long MarketId, int stockRemoveType, int pageindex = 1, int pagesize = 10)
            {
                try
                {
                    //条件从那个表中获取
                    var exp = Expressionable.Create<Market>();
                    exp.AndIF((MarketId != 0), m => m.MarketId == MarketId);
                    exp.And(m => m.IsDel == true);

                    //分页
                    var page = await sRepository.PageAsync(pageindex, pagesize, exp.ToExpression());

                    //总页数
                    var totalPage = (int)Math.Ceiling((double)page.Item1 / pagesize);
                    return new ResultDto<PagingDto<Market>>
                    {
                        Code = Status.Success,
                        Message = "成功",
                        Data = new PagingDto<Market>
                        {
                            TotalRows = page.Item1,
                            Rows = page.Item2,
                            TotalPage = totalPage,
                        }
                    };
                }
                catch (Exception e)
                {
                    return new ResultDto<PagingDto<Market>>
                    {
                        Code = Status.Failed,
                        Message = e.Message,
                    };
                    throw;
                }
            }

            /// <summary>
            /// 产品修改
            /// </summary>
            /// <returns></returns>
            [HttpPut]

            public async Task<ResultDto> UpdProduct(Market Market)
            {
                try
                {
                    var res = await sRepository.FistAsync(x => x.MarketId != Market.MarketId);
                    if (res == null)
                    {
                        await sRepository.UpdateAsync(Market);
                        return new ResultDto
                        {
                            Code = Status.Success,
                            Message = "产品信息修改成功！"
                        };
                    }
                    else
                    {
                        return new ResultDto
                        {
                            Code = Status.Success,
                            Message = "产品信息修改失败！"

                        };
                    }
                }
                catch (Exception e)
                {
                    return new ResultDto
                    {
                        Code = Status.Success,
                        Message = e.Message
                    };
                    throw;
                }
            }
            /// <summary>
            /// 返回查询到的单条产品信息
            /// </summary>
            /// <param name="id"></param>
            /// <returns></returns>
            [HttpGet]

            public async Task<ResultDto<Market>> BackProudtc(string id)
            {
                try
                {
                    var res = await sRepository.FistAsync(x => x.MarketId.ToString().Contains(id));
                    return new ResultDto<Market>
                    {
                        Code = Status.Success,
                        Data = res
                    };
                }
                catch (Exception e)
                {
                    return new ResultDto<Market>
                    {
                        Code = Status.Success,
                        Message = e.Message
                    };
                    throw;
                }
            }
            /// <summary>
            /// 产品删除
            /// </summary>
            /// <param name="id"></param>
            /// <returns></returns>
            [HttpDelete]

            public async Task<ResultDto<bool>> DeleteAsync(long id)
            {
                try
                {

                    var result = await sRepository.Delete(id);
                    if (result > 0)
                    {
                        return new ResultDto<bool>
                        {
                            Code = Status.Success,
                            Data = true,
                            Message = "删除成功！"
                        };
                    }
                    else
                    {
                        return new ResultDto<bool>
                        {
                            Code = Status.Failed,
                            Data = false,
                            Message = "删除失败"
                        };
                    }   
                }
                catch (Exception e)
                {
                    return new ResultDto<bool>
                    {
                        Code = Status.Failed,
                        Message = e.Message
                    };
                    throw;
                }
            }
        }
    }

}
